package com.ebook.eboo.Controller;

import com.ebook.eboo.Result.Result;
import com.ebook.eboo.Service.userService;
import com.ebook.eboo.pojo.User;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.jws.soap.SOAPBinding;
import java.util.List;
import java.util.Objects;

@Controller
public class loginController {

    @Autowired
    userService userService;

    @CrossOrigin  //跨域支持
    @PostMapping(value = "api/login")
    @ResponseBody
    public Result<User> login(@RequestBody User requestUser){

        String username = requestUser.getUsername();
        String password = requestUser.getPassword();
        List<User> user = userService.getByNameAndPass(username,password);
        if(user.size()==0){
            String message = "账号密码错误";
            System.out.println(message);
            return new Result<User>(400,user);
        }else{
            System.out.println(user);
            return new Result<User>(200,user);
        }
    }
    @CrossOrigin
    @PostMapping(value = "api/save")
    @ResponseBody
    public Result save(@RequestBody User requestUser){
        userService.saveUser(requestUser);
        return new Result(200);
    }
}
